/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.AddReport;
import controller.DeviceDetail;
import controller.DevicesOfLab;
import controller.ListDevices;
import controller.ListLabs;
import entities.Device;
import entities.Lab;
import entities.Report;
import java.util.List;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@ViewScoped
public class AddReportBean {

    private int labid;
    private int deviceid;
    private List<Lab> labs;
    private List<Device> devices;
    private String title;
    private String content;

    public int getLabid() {
        return labid;
    }

    public void setLabid(int labid) {
        this.labid = labid;
    }

    public int getDeviceid() {
        return deviceid;
    }

    public void setDeviceid(int deviceid) {
        this.deviceid = deviceid;
    }

    public List<Lab> getLabs() {
        ListLabs l = new ListLabs();
        return l.getLabs();
    }

    public void setLabs(List<Lab> labs) {
        this.labs = labs;
    }

    public List<Device> getDevices() {
        ListDevices ld = new ListDevices();
        return ld.getDevices();
    }

    public void setDevices(List<Device> devices) {
        this.devices = devices;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * Creates a new instance of AddReportBean
     */
    public AddReportBean() {
    }

    public String resultAdd() {
        DeviceDetail dd = new DeviceDetail();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        LoginBean lg = (LoginBean) app.createValueBinding("#{loginBean}").getValue(facesContext);
        Report r = new Report();
        r.setContent(content);
        r.setSender(lg.getUser());
        r.setStatus(Boolean.TRUE);
        r.setTitle(title);
        r.setDeviceID(dd.deviceDetail(deviceid));
        AddReport ar = new AddReport();
        boolean addReport = ar.addReport(r);
        if (addReport) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Success"));
            return "";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Error please try again later"));
        return "";
    }
}
